#!/usr/bin/perl
use warnings;
use strict;
use MP3::Tag;
use Data::Dumper;
use Getopt::Long;

my ($dump, $dumpraw, $listframes, $dumpframe, $deletetag, $listtags) = (0,0,0,0,undef,0); # $deletetag = undef = important
GetOptions("dump" => \$dump, "dump-raw" => \$dumpraw, "list-frames" => \$listframes, "dump-frame=s" => \$dumpframe, "delete-tag=i" => \$deletetag, "list-tags" => \$listtags);

if (!$dump && !$listframes && !$dumpframe && !defined $deletetag && !$listtags && !$dumpraw) {
	die "You need to specify either --dump, --dump-raw, --list-frames, --dump-frame XXXX, --list-tags or --delete-tag (1 or 2)";
}

if (!(scalar @ARGV)) {
	die "You need to specify at least one file!";
}

for my $file (@ARGV) {
	my $mp3 = MP3::Tag->new($file);
	die "Unable to open file $file, exiting!" unless $mp3;

	$mp3->get_tags() or die "Unable to read any ID3 tags!";

	if ($dumpraw) {
		print Dumper ( \$mp3 );
		exit 0;
	}

	if ($dump) {
		if (exists $mp3->{ID3v1}) {
			my $id3v1 = $mp3->{ID3v1};
			print "File has an ID3v1 tag:\n";
			print " Artist: " .$id3v1->artist . "\n";
			print "  Title: " .$id3v1->title . "\n";
			print "  Album: " .$id3v1->album . "\n";
			print "   Year: " .$id3v1->year . "\n";
			print "  Genre: " .$id3v1->genre . "\n";
			print "Comment: " .$id3v1->comment . "\n";
			print "  Track: " .$id3v1->track . "\n";
			print "\n";
		}
		if (exists $mp3->{ID3v2}) {
			my $id3v2 = $mp3->{ID3v2};
			print "File has an ID3v2 tag with the following frames/values:\n";
			
			my $frames = $id3v2->get_frame_ids();

			$Data::Dumper::Terse = 1;

			foreach my $frame (keys %$frames) {
				my ($info, $name, @rest) = $id3v2->get_frame($frame);
				if (ref $info) {
					print "$frame: " . Dumper ( $info );
				}
				else {
					print "$frame: $info\n";
				}
			}		
		}
		exit 0;
	}

	if ($listtags) {
		if (exists $mp3->{ID3v1}) {
			print "$file has an ID3v1 tag\n";
		}
		if (exists $mp3->{ID3v2}) {
			my $v2ver = $mp3->{ID3v2}->version();
			print "$file has an ID3v2.$v2ver tag\n";
		}
		exit 0;
	}

	if ($deletetag) {
		if ($deletetag != 1 && $deletetag != 2) {
			die "--delete-tag needs either 1 (ID3v1) or 2 (ID3v2) as an argument!";
		}

		if ($deletetag == 1) {
			if (exists $mp3->{ID3v1}) {
				if ($mp3->{ID3v1}->remove_tag()) {
					print "Removed ID3v1 tag successfully\n";
					exit 0;
				}
				else {
					print "Failed to remove ID3v1 tag!\n";
					exit 1;
				}
			}
			else {
				print "Track has no ID3v1 tag to remove!\n";
				exit 1;
			}
		}

		elsif ($deletetag == 2) {
			if (exists $mp3->{ID3v2}) {
				if ($mp3->{ID3v2}->remove_tag()) {
					print "Removed ID3v2 tag successfully\n";
					exit 0;
				}
				else {
					print "Failed to remove ID3v2 tag!\n";
					exit 1;
				}
			}
			else {
				print "Track has no ID3v2 tag to remove!\n";
				exit 1;
			}
		}
	
	}
	if ($dumpframe) {
		my $v2 = $mp3->{ID3v2} if exists $mp3->{ID3v2};
		if (!defined $v2) {
			die "No ID3v2 tag found!";
		}
	
		my $frame = $v2->get_frame($dumpframe);
		if (!defined $frame || length $frame < 1) {
			die "File $file doesn't appear to contain any frame named $dumpframe";
		}

		print "$dumpframe: ";
		binmode STDOUT;
		$Data::Dumper::Terse = 1;
		print Dumper ( \$frame );
		exit 0;
	}

	if ($listframes) {
		my $v2 = $mp3->{ID3v2} if exists $mp3->{ID3v2};
		if (!defined $v2) {
			die "No ID3v2 tag found!";
		}
		my $frames = $v2->get_frame_ids();
		#while (my ($k, $v) = each(%{$frames})) {
		print "File $file contains the following ID3v2 frames:\n";
		for my $k (sort keys %{$frames}) {
			print "$k: " . $frames->{$k} . "\n";
		}
		exit 0;
	}
}
